package com.tkjt.pms.modules.scoring.service.impl;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.mapper.SysConfigMapper;
import org.jeecg.modules.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.EsJsonUtils;
import com.tkjt.pms.common.utils.EsStringUtils;
import com.tkjt.pms.common.vos.RequestJson;
import com.tkjt.pms.modules.common.service.TkDataConfigService;
import com.tkjt.pms.modules.scoring.model.GradeDetailDatagridResponseModel;
import com.tkjt.pms.modules.scoring.model.GradeViewDatagridResponseModel;
import com.tkjt.pms.modules.scoring.model.ScoreCommentQueryRequestModel;
import com.tkjt.pms.modules.scoring.model.ScoreCommentQueryResponseModel;
import com.tkjt.pms.modules.scoring.model.ScoreCommentSaveRequestModel;
import com.tkjt.pms.modules.scoring.model.ScoreDatagridRequestModel;
import com.tkjt.pms.modules.scoring.model.ScoreValueSaveRequestModel;
import com.tkjt.pms.modules.scoring.service.TkScoringBizService;
import com.tkjt.pms.modules.scoring.service.TkScoringService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@Transactional
public class TkScoringServiceImpl implements TkScoringService {
	
    @Autowired
    private TkDataConfigService tkDataConfigService;
    
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private Map<String, TkScoringBizService> tkScoringBizServiceBaseMap = new HashMap<>();

    @Override
    public Result<?> indexCodes(LoginUser loginUser) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        TkScoringBizService tkScoringBizService = checkService(dataType);
        return tkScoringBizService.indexCodes(loginUser);
    }

    @Override
    public Result<?> scoreDatagrid(LoginUser loginUser,ScoreDatagridRequestModel model) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        TkScoringBizService tkScoringBizService = checkService(dataType);
        return tkScoringBizService.scoreDatagrid(model,dataType);
    }

    @Override
    public Result<?> saveRows(LoginUser loginUser,ScoreValueSaveRequestModel model) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        TkScoringBizService tkScoringBizService = checkService(dataType);
        return tkScoringBizService.saveRows(model,loginUser,dataType);
    }

    @Override
    public Result<?> scoreCommentSave(LoginUser loginUser, ScoreCommentSaveRequestModel model) throws Exception {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        TkScoringBizService tkScoringBizService = checkService(dataType);
        return tkScoringBizService.scoreCommentSave(loginUser, model,dataType);
    }

    @Override
    public GradeViewDatagridResponseModel gradeViewDatagrid(LoginUser loginUser,HttpServletRequest request) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        TkScoringBizService tkScoringBizService;
        RequestJson requestJsonObject = EsJsonUtils.getRequestJsonObject(request);
        String year = requestJsonObject.getString("indexYear");
        /*RequestJson requestJsonObject = EsJsonUtils.getRequestJsonObject(request);
        String year = requestJsonObject.getString("indexYear");
        if(!EsDateUtils.getIndexYearMonth(new Date()).substring(0,4).equals(year)){
            String serviceName = "tkScoringBizServiceMirror";
            tkScoringBizService = tkScoringBizServiceBaseMap.get(serviceName);
        }else{
            tkScoringBizService = checkService(dataType);
        }*/
        tkScoringBizService = checkService(dataType);
        return tkScoringBizService.gradeViewDatagrid(year,dataType);
    }

    @Override
    public ScoreCommentQueryResponseModel scoreCommentQuery(LoginUser loginUser,ScoreCommentQueryRequestModel model) throws Exception {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        TkScoringBizService tkScoringBizService = checkService(dataType);
        return tkScoringBizService.scoreCommentQuery(model,dataType);
    }

    @Override
    public GradeDetailDatagridResponseModel gradeDetailDatagrid(LoginUser loginUser,HttpServletRequest request) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        TkScoringBizService tkScoringBizService;
        RequestJson requestJsonObject = EsJsonUtils.getRequestJsonObject(request);
        String yearMonth = requestJsonObject.getString("indexYearMonth");
        String indexDepart = requestJsonObject.getString("indexDepart");//被评分部门
        String indexScorer = requestJsonObject.getString("scorerCode");//被评分人
        String  isTotal = requestJsonObject.getString("isTotal");//true  为简报
        int pageNo = requestJsonObject.getPageNo();
        int pageSize = requestJsonObject.getPageSize();
        Map<String,Object> requestParam = new HashMap<>();
        requestParam.put("isTotal",isTotal);
        requestParam.put("yearMonth",yearMonth);
        requestParam.put("indexDepart",indexDepart);
        requestParam.put("indexScorer",indexScorer);
        requestParam.put("pageNo",pageNo);
        requestParam.put("pageSize",pageSize);
        String year = yearMonth.substring(0,4);
        tkScoringBizService = checkService(dataType);
        /*if(!EsDateUtils.getIndexYearMonth(new Date()).substring(0,4).equals(year)){
            String serviceName = "tkScoringBizServiceMirror";
            tkScoringBizService = tkScoringBizServiceBaseMap.get(serviceName);
        }else{
            tkScoringBizService = checkService(dataType);
        }*/
        return tkScoringBizService.gradeDetailDatagrid(requestParam,dataType);
    }

    /**
     * 我的绩效-指标图表(达成率)
     * @param request
     * @return
     */
    @Override
    public Result<?> gradeIndexChart(LoginUser loginUser,HttpServletRequest request) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        TkScoringBizService tkScoringBizService = checkService(dataType);
        return tkScoringBizService.gradeIndexChart(request,dataType);
    }

    @Override
    public Result<?> gradeIndexChartNew(LoginUser loginUser, HttpServletRequest request) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        RequestJson requestJsonObject = EsJsonUtils.getRequestJsonObject(request);
        String yearMonth = requestJsonObject.getString("indexYearMonth");
        if(StringUtils.isEmpty(yearMonth)){
            return Result.error("参数缺失");
        }
        String indexDepart = requestJsonObject.getString("indexDepart");
        if(StringUtils.isEmpty(indexDepart)){
            return Result.error("参数缺失");
        }
        String indexScorer = requestJsonObject.getString("scorerCode");
        if(StringUtils.isEmpty(indexScorer)){
            return Result.error("参数缺失");
        }
        String indexCode = requestJsonObject.getString("indexCode");
        String indexValueCode = requestJsonObject.getString("indexValueCode");
        if(StringUtils.isEmpty(indexValueCode)){
            return Result.error("参数缺失");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("yearMonth",yearMonth);
        map.put("indexDepart",indexDepart);
        map.put("indexScorer",indexScorer);
        map.put("indexCode",indexCode);
        map.put("indexValueCode",indexValueCode);
        TkScoringBizService tkScoringBizService = checkService(dataType);
        return tkScoringBizService.gradeIndexChartNew(map,dataType);
    }

    public TkScoringBizService checkService(String dataType) {
        String serviceName = "tkScoringBizService" + EsStringUtils.onlyFirstCharUpper(dataType.toUpperCase());
        return tkScoringBizServiceBaseMap.get(serviceName);
    }
    
    @Override
    public Result<?> gradeEvents(LoginUser loginUser){
    	
    	String strGradeEvents = sysConfigMapper.queryConfigByCode("gradeEvents");
    	if(StringUtils.isEmpty(strGradeEvents)) {
    		strGradeEvents = "[]";
    	}
    	//log.info(strGradeEvents);
    	JSONArray jsonGradeEvents = (JSONArray) JSONArray.parse(strGradeEvents);
        String userName = loginUser.getUsername();
        List<String> roleCodes = sysUserRoleMapper.getRoleByUserName(userName);
        if(roleCodes.contains("ykgm") || roleCodes.contains("ykkpiowner")){
            Map<String,Object> other = new HashMap<>();
            other.put("code","GE00000");
            other.put("name","其他问题");
            jsonGradeEvents.add(other);
        }
    	Map<String, Object> gradeEvents = new HashMap<String, Object>();
    	gradeEvents.put("gradeEvents", jsonGradeEvents);
    	
    	return Result.ok(gradeEvents);
    }
}
